// pages/order/order.js
const util = require('../../utils/util.js')
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
      user_name:'',
      user_tel:'',
      area:'',
      address:'',
      message:'暂无备注',
      user_id:'',
      number:'',
      yuyue_time:'',
      date: '',  //初始化时间
      region: ['', '', ''],
      startTime:''
  },
    bindRegionChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            region: e.detail.value,
          area: e.detail.value[0]+e.detail.value[1] + e.detail.value[2]
        })

    },
    bindDateChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            date: e.detail.value,
            yuyue_time: e.detail.value
        })
    },
    nameInput:function(e){
        this.setData({
            user_name:e.detail.value
        })
    },
    phoneInput: function (e) {
        this.setData({
            user_tel: e.detail.value
        })
    },
    addressInput: function (e) {
        this.setData({
            address: e.detail.value
        })
    },
    postOrder:function(){
        if (!this.data.user_name){
            wx.showToast({
                title: '收件人不能为空',
                icon:'none'
            })
            return false
        }
        var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
      if (!myreg.test(this.data.user_tel)){
        wx.showToast({
          title: '请输入正确的手机号',
          icon: 'none'
        })
        return false
      }
        if (!this.data.area) {
            wx.showToast({
                title: '城市不能为空',
                icon: 'none'
            })
            return false
        }
        if (!this.data.address) {
            wx.showToast({
                title: '详细地址不能为空',
                icon: 'none'
            })
            return false
        }
        if (!this.data.yuyue_time) {
            wx.showToast({
                title: '配送时间不能为空',
                icon: 'none'
            })
            return false
        }

        var data = {
            user_name: this.data.user_name,
            user_tel: this.data.user_tel,
            area: this.data.area,
            address: this.data.address,
            message: this.data.message,
            user_id: this.data.user_id,
            number: this.data.number,
            yuyue_time: this.data.yuyue_time,
        }
        app.request('api/VerCode/AddOrderInfo', data, 'Get',
            (result) => {
                if (result.data.Code == '0000') {
                  wx.showModal({
                    title: '提示',
                    content: '订单提交成功',
                    showCancel: false,
                    success(res) {
                      if (res.confirm) {
                        wx.switchTab({
                          url: '/pages/my/my'
                        })
                      } else if (res.cancel) {
                        console.log('用户点击取消')
                      }
                    }
                  })

                } else {
                    wx.showToast({
                        title: '服务器维护中，请稍后！',
                        icon: 'none',
                    })
                }
            }
        );
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      console.log(options.cardNum)

      var xx = Date.now();
      var yy = util.formatTime(new Date(xx))
      var start = yy.substr(0, 10).replace(/(\/)/g,'-'); 
      console.log(start)
      this.setData({
          startTime: start,
          number: options.cardNum,
      })

      var data = {
          open_id: wx.getStorageSync('openId')
      }
      app.request('api/VerCode/GetUserInfo', data, 'Get',
          (result) => {
            console.log(result.data.User_Id)
            this.setData({
              user_id: result.data.User_Id
            })
              // if (result.data.Code == '0000') {
                
              // } else {
              //     wx.showToast({
              //         title: '服务器维护中，请稍后！',
              //         icon: 'none',
              //     })
              // }
          }
      );
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})